<template>
  <div class='product-list-container'>
    <div class="product-list">
      <h2>产品列表</h2>
      <ul>
        <li v-for="(item, index) in productListData.list"
            :key="index"
            class="product-list-item">
          <img :src="item.product_img"
               class="product-img"
               alt="">

          <div class="product-info-container">
            <div class="product-info">
              <p class="product-name">{{item.product_name}}</p>
            </div>
            <p class="product-subtitle">
              {{item.product_desc}}
            </p>
            <div class="product-description-container">
              <span>使用描述：</span>
              <div class="product-description">
                <p>{{item.product_man}}</p>
              </div>
            </div>

            <div class="product-price">
              <span>{{item.product_price}}</span><i>元</i>
            </div>
            <div class="btn-group">
              <div class="product-num">
                <el-input-number v-model="item.num"
                                 @change="calCartPrice"
                                 @blur="calCartPriceBlur(item)"
                                 size="mini"
                                 :min="1"
                                 :precision="0"
                                 :max="1000"></el-input-number>
              </div>
              <button @click="addCart(item)">添加清单</button>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="product-cart">
      <div class="product-cart-header">
        <p>购物清单</p>
        <span v-if="productCart.length>0"
              @click="removeAll">清空列表</span>
      </div>
      <div class="cart-list-container">
        <div v-if="productCart.length===0"
             class="product-cart-placeholder">
          <img src="@/assets/cart-placeholder.png"
               alt="">
          <p>购物清单还是空的</p>
        </div>
        <ul v-if="productCart.length>0"
            class="product-cart-list"
            :class="{'scroll':productCart.length>4}">
          <li class="product-cart-item"
              v-for="(item) in productCart"
              :key="item.product_id">
            <img :src="item.product_img"
                 alt="">
            <div class="cart-info-container">
              <div class="product-cart-info">
                <p class="product-name">{{item.product_name}}</p>
                <span class="product-num">x {{item.num}}</span>
              </div>
              <div class="product-cart-info">
                <span class="product-price">￥ {{item.product_price}}</span>
                <span @click="removeCart(item)"
                      class="delete">删除</span>
              </div>
            </div>

          </li>
        </ul>
      </div>
      <div class="buy-now">
        <span class="num">{{productCartData.totalNum}}件</span>
        <div class="btn-group">
          <span :class="{'disabled':productCart.length===0}"
                class="total-price">￥{{productCartData.totalPrice}}</span>
          <button @click="submitOrder"
                  :class="{'disabled':productCart.length===0}"
                  :disabled="productCart.length===0"
                  class="buy">立即购买</button>
        </div>

      </div>
    </div>
  </div>
</template>
<script>
import { ProductListApi } from '@/services/PurchaseMarket'
export default {
  data () {
    return {
      // 产品列表
      productListData: {
        list: [

        ]
      },
      // 清单列表
      productCart: [],
      // 清单数据，包括数量和总价
      productCartData: {
        totalNum: 0,
        totalPrice: 0
      },
      getProductListParam: {
        uuid: 1,
        limit: 10,
        page: 1
      },
      num: 0
    }
  },
  methods: {
    // 添加到清单
    addCart (item) {
      let flag = this.isInCart(item)

      if (!flag) {
        this.productCart.push(item)
      } else {
        this.$message.warning('该商品已添加到清单中')
      }
      this.calCartPrice()
    },
    // 判断商品是否已在清单中
    isInCart (product) {
      let flag = false
      this.productCart.map((item) => {
        if (product.product_id === item.product_id) flag = true
      })
      return flag
    },
    // 数量选择框失去焦点操作
    calCartPriceBlur (item) {
      console.log(item)
      if (!item.num) item.num = 1
      this.calCartPrice()
    },
    // 计算清单总数量和总价
    calCartPrice (num) {
      console.log(num, '123')
      if (!num) num = 1
      this.productCartData.totalNum = 0
      this.productCartData.totalPrice = 0
      if (this.productCart.length > 0) {
        this.productCart.map((item) => {
          this.productCartData.totalNum += item.num
          this.productCartData.totalPrice += item.num * item.product_price
        })
        this.productCartData.totalPrice = this.productCartData.totalPrice.toFixed(2)
      }
    },
    // 从清单中移除
    removeCart (productItem) {
      let ind = -1
      this.productCart.map((item, index) => {
        if (item.product_id === productItem.product_id) ind = index
      })
      if (ind >= 0) {
        this.productCart.splice(ind, 1)
      }
      this.calCartPrice()
    },
    // 清空列表
    removeAll () {
      this.productCart.length = 0
      this.calCartPrice()
    },
    // 判断指定产品在清单中的index
    getCartIndex (product) {
      let productIndex = -1
      this.productCart.map((item, index) => {

      })
    },
    // 提交订单
    submitOrder () {
      let len = this.productCart.length
      if (len > 0) {
        window.localStorage.setItem('productCart', '')
        window.localStorage.setItem('productCart', JSON.stringify(this.productCart))
        this.$router.push({
          path: '/confirmOrder',
          query: {
            uuid: this.getProductListParam.uuid
          }
        })
      } else {
        this.$message.warning('请选择商品')
      }
    },
    // 获取产品列表
    getProductList () {
      ProductListApi.getProductList({
        params: this.getProductListParam
      }).then(res => {
        console.log(res, 'get product list')
        let data = res.data
        if (data.data) {
          this.productListData.list = data.data
          this.productListData.list.map((item) => {
            // item.num = 1
            this.$set(item, 'num', 1)
            item.product_price = parseFloat(item.product_price) / 100
          })
        }
      }, err => {
        console.log(err)
      })
    }
  },
  created () {
    console.log(this.$route.query, '11')
    this.getProductListParam.uuid = this.$route.query.uuid
    this.getProductList()
  },
  mounted () {
  }
}
</script>
<style scoped lang='scss'>
@function p2vw($size) {
  @return ($size/1108) * 100vw;
}

.product-list-container {
  width: 100%;
  min-height: 100%;
  display: flex;
  background: #efeff4;
  overflow-x: hidden;
  position: relative;
  // justify-content: space-between;
  i {
    font-style: normal;
  }
  p {
    margin: 0;
  }
  .product-list {
    background: #fff;
    // width: p2vw(762);
    width: calc((100% - 20px) * 0.7);
    h2 {
      height: 50px;
      padding-left: 20px;
      border-bottom: 1px solid #d9d9d9;
      font-size: 16px;
      color: #1441b3;
      line-height: 50px;
      margin: 0;
      font-weight: normal;
    }
    ul {
      padding: 40px 20px 0;
      .product-list-item {
        // width: p2vw(762);
        width: 100%;
        height: 160px;
        display: flex;
        margin-bottom: 40px;
        .product-img {
          width: 160px;
          height: 160px;
          border: 1px solid #bfbfbf;
          display: flex;
          justify-content: center;
          align-items: center;
          img {
            width: 160px;
            height: 160px;
            border: 0;
            outline: 0;
          }
        }
        .product-info-container {
          height: 160px;
          padding: 0 20px;
          flex-grow: 1;
          position: relative;

          p {
            margin: 0;
          }
          .product-info {
            width: 100%;
            display: flex;
            justify-content: space-between;
            align-items: center;
            margin-bottom: 10px;
            .product-name {
              color: #333;
              font-size: 16px;
              font-weight: bold;
            }
          }
          .product-price {
            position: absolute;
            left: 20px;
            bottom: 0;

            span {
              font-size: 24px;
              color: #ff4d4f;
              margin-right: 8px;
            }
            i {
              font-size: 12px;
              color: #666;
            }
          }
          .product-subtitle {
            margin-bottom: 10px;
            color: #999;
            font-size: 12px;
          }
          .product-description-container {
            margin-bottom: 20px;
            font-size: 12px;
            color: #666;
            line-height: 18px;
            span {
              width: 90px;
            }
            .product-description {
              width: 300px;
            }
          }
          .product-num {
            display: flex;
            justify-content: flex-start;
            span {
            }
            input {
            }
          }
          .btn-group {
            width: 250px;
            height: 48px;
            position: absolute;
            bottom: 0;
            right: p2vw(20);

            display: flex;
            justify-content: space-between;
            align-items: flex-end;
            button {
              width: 96px;
              height: 36px;
              background-color: #1890ff;
              color: #fff;
              font-size: 14px;
              border: 0;
              border-radius: 4px;
              cursor: pointer;
            }
          }
        }
      }
    }
  }
  .product-cart {
    // width: p2vw(326);
    width: calc((100% - 20px - 290px) * 0.3);
    height: 600px;
    background: #fff;
    border-radius: 4px 4px 0 0;
    position: fixed;
    right: 35px;
    top: 120px;
    box-sizing: border-box;
    .product-cart-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 50px;
      padding: 0 20px;
      border-bottom: 1px solid #d9d9d9;
      p {
        font-size: 16px;
        color: #1441b3;
      }
      span {
        font-size: 14px;
        color: #666;
        cursor: pointer;
      }
    }
    .cart-list-container {
      display: flex;
      justify-content: center;
      align-items: center;
      min-height: 450px;
      width: 100%;
      .product-cart-placeholder {
        img {
          width: 82px;
          height: 76px;
          margin-bottom: 13px;
        }
        p {
          font-size: 14px;
          color: #999;
          text-align: center;
        }
      }
      .product-cart-list {
        padding-top: 40px;
        height: 450px;
        width: 100%;
        &.scroll {
          overflow: scroll;
          overflow-x: hidden;
          /*滚动条样式*/
          &::-webkit-scrollbar {
            /*滚动条整体样式*/
            width: 4px; /*高宽分别对应横竖滚动条的尺寸*/
            height: 4px;
            opacity: 0;
          }
          &::-webkit-scrollbar-thumb {
            /*滚动条里面小方块*/
            border-radius: 5px;
            -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
            background: rgba(0, 0, 0, 0.2);
            opacity: 0;
          }
          &::-webkit-scrollbar-track {
            /*滚动条里面轨道*/
            -webkit-box-shadow: inset 0 0 5px rgba(0, 0, 0, 0.2);
            border-radius: 0;
            background: rgba(0, 0, 0, 0.1);
            opacity: 0;
          }
        }
        .product-cart-item {
          height: 80px;
          width: 100%;
          display: flex;
          padding: 0 20px;
          justify-content: space-between;
          align-items: center;
          box-sizing: border-box;
          margin-bottom: 40px;
          img {
            width: 60px;
            height: 60px;
            margin-right: 20px;
            border: 1px solid #bfbfbf;
          }
          .cart-info-container {
            height: 60px;
            display: flex;
            flex-direction: column;
            justify-content: space-between;
            flex-grow: 1;
            .product-cart-info {
              display: flex;
              align-items: center;
              justify-content: space-between;
              .product-name {
                font-size: 14px;
                color: #333;
              }
              .product-num {
                font-size: 14px;
                color: #666;
              }
              .product-price {
                font-size: 14px;
                color: #ff4d4f;
                font-weight: bold;
              }
              .delete {
                font-size: 12px;
                color: #666;
                cursor: pointer;
              }
            }
          }
        }
      }
    }
    .buy-now {
      // width: p2vw(326);
      width: 100%;
      height: 64px;
      position: absolute;
      bottom: 0;
      left: 0;
      box-sizing: border-box;
      box-shadow: 0px -2px 4px 0px rgba(20, 65, 179, 0.2);
      background: #fff;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 20px;
      .num {
        width: 50px;
        font-size: 14px;
        color: #333;
        margin-right: p2vw(47);
      }
      .btn-group {
        .total-price {
          width: 100px;
          font-size: 18px;
          color: #ff4d4f;
          margin-right: p2vw(20);
          font-weight: bold;
          &.disabled {
            color: #bfbfbf;
          }
        }
        .buy {
          width: 96px;
          height: 36px;
          background: #1890ff;
          border-radius: 4px;
          line-height: 36px;
          color: #fff;
          text-align: center;
          font-size: 14px;
          border: 0;
          &.disabled {
            background: #bfbfbf;
          }
        }
      }
    }
  }
}
</style>
